import logging
import os 
from progressbar import * 
from urllib import request
import time  
import requests
import threading
import pathlib
#from hashlib import blake2b as hash_
from hashlib import md5 as hash_
import math
import sqlite3

#设置日志文件的句柄和日志级别
logger = logging.getLogger('baidubce.http.bce_http_client')
fh = logging.FileHandler("sample.log")
fh.setLevel(logging.DEBUG)

#设置日志文件输出的顺序、结构和内容
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(fh)

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s  %(message)s #%(levelname)s#%(process)d_%(module)s_%(funcName)s')
root=pathlib.Path.cwd()
save_root =pathlib.Path.home( ) / 'AppData/LocalLow/Ludeon Studios/RimWorld by Ludeon Studios/'


def getHash(filepath):
    if(os.path.isfile(filepath)):
        size = os.path.getsize(filepath)
        # print(file,size)
        try:
            file = open(filepath, "rb")
        except:
            logging.error("can't open file!")
            return "fail!"
        hash = hash_()
        n = 0
        bs = 1024*1024*32
        ab = math.ceil(size/bs)
        while True :
            # 处理该行的代码
            data = file.read(bs)
            if(len(data) == 0):
                break
            hash.update(data)
        file.close()
        return hash.hexdigest()
        # return hash.hexdigest()
    else:
        logging.error("error")


print(getHash(os.path.expanduser('~/AppData/LocalLow/Ludeon Studios/RimWorld by Ludeon Studios/Saves/Autosave-2.rws')))
